package kov.core.body.zy
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	import kov.core.BodyState;
	import kov.core.IBody;
	import kov.core.StateCommand;
	import kov.core.body.StateEnum;
	import kov.core.global.Global;
	import kov.core.global.KeyConfig;
	import kov.events.KeyEvent;
	import kov.ui.KeyCombo;
	import kov.ui.KeyDef;
	
	public class ZYXingZou extends BodyState
	{
		[Embed("../../../../../assets/zy/xingzou/0.gif")]
		private var Frame0:Class;
		[Embed("../../../../../assets/zy/xingzou/1.gif")]
		private var Frame1:Class;
		[Embed("../../../../../assets/zy/xingzou/2.gif")]
		private var Frame2:Class;
		[Embed("../../../../../assets/zy/xingzou/3.gif")]
		private var Frame3:Class;
		[Embed("../../../../../assets/zy/xingzou/4.gif")]
		private var Frame4:Class;
		[Embed("../../../../../assets/zy/xingzou/5.gif")]
		private var Frame5:Class;
		[Embed("../../../../../assets/zy/xingzou/6.gif")]
		private var Frame6:Class;
		[Embed("../../../../../assets/zy/xingzou/7.gif")]
		private var Frame7:Class;
		
		/**
		 * 构造函数。
		 * @param body
		 * @param priority
		 */		
		public function ZYXingZou(body:IBody, priority:int = 1)
		{
			super(StateEnum.XING_ZOU, body, priority);
			createRenderer();
			createKeyCombo();
		}
		
		private function createRenderer():void
		{
			var f0:BitmapData = (new Frame0() as Bitmap).bitmapData;
			var f1:BitmapData = (new Frame1() as Bitmap).bitmapData;
			var f2:BitmapData = (new Frame2() as Bitmap).bitmapData;
			var f3:BitmapData = (new Frame3() as Bitmap).bitmapData;
			var f4:BitmapData = (new Frame4() as Bitmap).bitmapData;
			var f5:BitmapData = (new Frame5() as Bitmap).bitmapData;
			var f6:BitmapData = (new Frame6() as Bitmap).bitmapData;
			var f7:BitmapData = (new Frame7() as Bitmap).bitmapData;
			this.renderer.push(f0);
			this.renderer.push(f1);
			this.renderer.push(f2);
			this.renderer.push(f3);
			this.renderer.push(f4);
			this.renderer.push(f5);
			this.renderer.push(f6);
			this.renderer.push(f7);
		}
		
		private var keyStateHash:Dictionary;
		
		// 创建按键。
		private function createKeyCombo():void
		{
			this.keyStateHash = new Dictionary();
			
			// 放血
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([
				KeyDef.create(KeyConfig.A, KeyDef.DOWN),
				KeyDef.create(KeyConfig.B, KeyDef.DOWN)
			]), false))] = StateEnum.FANG_XUE;
			
			// 防守
			var id:uint = Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([KeyDef.create(KeyConfig.C)])));
			this.keyStateHash[id] = StateEnum.FANG_SHOU;
			
			// 打
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([
				KeyDef.create(KeyConfig.A)
			])))] = StateEnum.DA;
			
			// 蹲
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([
				KeyDef.create(KeyConfig.DOWN),
				KeyDef.create(KeyConfig.B)
			])))] = StateEnum.DUN;
			
			// 跳
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([
				KeyDef.create(KeyConfig.B)
			])))] = StateEnum.TIAO;
		}
		
		override public function execute():void
		{
			this.body.renderer.frameSequence = this.renderer;
			this.body.renderer.delay = 90;
			this.body.renderer.offsetX = 60;
			this.body.renderer.offsetY = 83;
			this.body.renderer.gotoAndPlay(0);
			
			Global.stage.addEventListener(Event.ENTER_FRAME, loop);
			Global.key.addEventListener(KeyEvent.WORK, onKeyWork);
		}
		
		override public function exit():void
		{
			Global.stage.removeEventListener(Event.ENTER_FRAME, loop);
			Global.key.removeEventListener(KeyEvent.WORK, onKeyWork);
		}
		
		private function loop(event:Event):void
		{
			var isLeft:Boolean = Global.key.isDown(KeyConfig.LEFT);
			var isRight:Boolean = Global.key.isDown(KeyConfig.RIGHT);
			var isUp:Boolean = Global.key.isDown(KeyConfig.UP);
			var isDown:Boolean = Global.key.isDown(KeyConfig.DOWN);
			
			if ( !(isLeft || isRight || isUp || isDown) ) // 方向键都没有按下
			{
				var cmd:StateCommand = new StateCommand(StateEnum.BEI_ZHAN);
				this.body.executeStateCommand(cmd);
				return;
			}
			
			var bodyX:Number = this.body.x;
			var bodyY:Number = this.body.y;
			var toX:Number = bodyX;
			var toY:Number = bodyY;
			if (isLeft)
			{
				this.body.faceToLeft = true;
				toX = bodyX - 2;
			}
			else if (isRight)
			{
				this.body.faceToLeft = false;
				toX = bodyX + 2;
			}
			if ( !Global.world.mapData.blockData.isBlock(toX, bodyY) )
			{
				this.body.x = toX;
			}
			
			if (isUp)
			{
				toY = bodyY - 1;
			}
			else if (isDown)
			{
				toY = bodyY + 1;
			}
			if ( !Global.world.mapData.blockData.isBlock(this.body.x, toY) )
			{
				this.body.y = toY;
			}
		}
		
		// 按键事件。
		private function onKeyWork(event:KeyEvent):void
		{
			var stateName:String = this.keyStateHash[event.keyComboID];
			if (!stateName)
			{
				return;
			}
			
			// 防守状态：只有左右键按住时才可进入防守状态，否则返回。
			if (stateName == StateEnum.FANG_SHOU && !Global.key.isDown(KeyConfig.LEFT) && !Global.key.isDown(KeyConfig.RIGHT) )
			{
				return;
			}
			
			var cmd:StateCommand = new StateCommand(stateName);
			this.body.executeStateCommand(cmd);
		}
		
	}
}